package com.loe.test

import android.graphics.Color
import android.net.Uri
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.widget.FrameLayout
import android.widget.LinearLayout
import android.widget.Toast
import androidx.annotation.Keep
import com.binioter.guideview.Component
import com.binioter.guideview.Guide
import com.binioter.guideview.GuideBuilder
import com.google.android.exoplayer2.*
import com.google.android.exoplayer2.source.DefaultMediaSourceFactory
import com.loe.http.LoeHttp
import com.loe.mvp.BaseModel
import com.loe.mvp.BaseModelPresenter
import com.loe.mvp.ext_app.inflate
import com.loe.mvp.ext_app.setStatusAlpha
import com.loe.mvp.ext_app.setStatusBar
import com.loe.mvp.ext_java.*
import com.loe.mvp.ext_view.setDelayClickListener
import com.loe.mvp.ext_view.visible
import com.loe.mvp.mvp.BaseMvpActivity
import com.loe.mvp.util.*
import com.loe.router.Router
import com.zzhoujay.richtext.RichText
import kotlinx.android.synthetic.main.activity_model.*
import kotlinx.android.synthetic.main.layout_guide1.view.*


@Router
class TestActivity : BaseMvpActivity<TestPresenter, TestModel>()
{
    var goText by bindString { buttonGo }

    val go by observeString { buttonGo }

    var param = ""

    override fun onCreate(savedInstanceState: Bundle?)
    {
        super.onCreate(savedInstanceState)
        setStatusAlpha()
        setContentView(R.layout.activity_model)

        param = intent.getStringExtra("param") ?: ""

        buttonGo.text = buttonGo.text.toString() + param

        val popuper = PopuperList(activity)

        popuper.setStrings("一", "二as啊", "三搜索稍等", "飒", "搜索五")


        MyPlayer.setOnStart { buttonGo.setTextColor(Color.BLUE) }
        MyPlayer.setOnStop { buttonGo.setTextColor(Color.BLACK) }
        MyPlayer.setOnItemStart { toast("切换 $it") }

        buttonGo.setDelayClickListener(0, 200)
        {
            MyPlayer.play(this, listOf(
                PlayBean("http://192.168.191.1:8080/test/voice/a1.mp3", 11),
                PlayBean("http://192.168.191.1:8080/test/voice/a2.mp3", 12),
                PlayBean("http://192.168.191.1:8080/test/voice/a3.mp3", 13),
                PlayBean("http://192.168.191.1:8080/test/voice/a4.mp3", 14),
            ))
        }



        buttonNew.setOnClickListener()
        {
            toast("从业资格证姓名与用户的身份证姓名不一致,上传失败!")

        }

        SpCache.name = "sdfsdfsdf稍等代付款可视对讲快捷键可快速开发大健康鼓风机客服管家看电视的收到货款时代光华可可粉高科技圣诞节kg交付给"
        SpCache.address = "健康打卡点附近给对方加咖啡馆风格单联单控时令水果吉林省倒计时打卡了房间胜多负少的开放式楼房考虑的看法见识到了扣水电费集福卡了水电费"

//        var mmkv = MMKV.defaultMMKV()
//        mmkv.encode("name", "sdfsdfsdf稍等代付款可视对讲快捷键可快速开发大健康鼓风机客服管家看电视的收到货款时代光华可可粉高科技圣诞节kg交付给")
//        mmkv.encode("address", "健康打卡点附近给对方加咖啡馆风格单联单控时令水果吉林省倒计时打卡了房间胜多负少的开放式楼房考虑的看法见识到了扣水电费集福卡了水电费")

        buttonStartService.setOnClickListener()
        {
//            val start = System.currentTimeMillis()
//
//            for (i in 0..10000)
//            {
////                val temp = mmkv.decodeString("name")
////                mmkv.encode("name", mmkv.decodeString("address"))
////                mmkv.encode("address", temp)
//
////                val temp = SpCache.getString("name")
////                SpCache.putString("name", SpCache.getString("address"))
////                SpCache.putString("address", temp)
//            }
//
//            textView.text = "耗时：${(System.currentTimeMillis() - start) / 1000.0}秒"
        }

        buttonLoad.setOnClickListener()
        {
            LoeHttp.get("http://192.168.191.1:8080/test/demo.html")
//            LoeHttp.get("https://www.baidu.com")
                .okString()
                {
                    Log.d("runtime", it)
                    RichText.fromHtml(it).into(textHtml)
                }
        }

        RichText.fromHtml(richData).into(textHtml)
    }

    override fun initView()
    {
    }

    /**
     * Presenter需要反射调用的方法，@Keep保持不被混淆
     */
    @Keep
    fun goHome(s: String): String
    {
        buttonGo.text = "哈哈哈$s~  goHome！！"
        return "我是返回"
    }

    @Keep
    fun goMain(): String
    {
        buttonNew.visible = false
        toast("11111main哒哒哒哒哒哒")
        return "傻了吧唧"
    }

    private fun showGuideView()
    {
        var guide: Guide? = null

        val builder = GuideBuilder()
            .setTargetView(buttonGo)
            .setAlpha(150)
            .setHighTargetCorner(20)
            .setHighTargetPadding(10)
            .setAutoDismiss(false)
        builder.setOnVisibilityChangedListener(object : GuideBuilder.OnVisibilityChangedListener
        {
            override fun onShown()
            {
            }

            override fun onDismiss()
            {
                toast("showGuideView2()")
            }
        })
        builder.addComponent(object : Component
        {
            override fun getView(inflater: LayoutInflater?): View
            {
                val layout = inflate(R.layout.layout_guide1, null)
                layout.buttonNext.setOnClickListener()
                {
                    guide?.dismiss()
                }
                return layout
            }

            override fun getAnchor(): Int = Component.ANCHOR_BOTTOM

            override fun getFitPosition(): Int = Component.FIT_START

            override fun getXOffset(): Int = 0

            override fun getYOffset(): Int = 15
        })
        guide = builder.createGuide()
        guide.show(this)

    }
}

class TestPresenter : BaseModelPresenter<TestModel>()
{
    override fun onInit()
    {
//        model.same()
    }

    fun go()
    {
//        val s = "goHome".invokeToString(233, "顺丰到付都是")
//        toast(s)
//        "goMain".invoke()

        model.same()
    }
}

class TestModel : BaseModel()
{
    fun same()
    {
//        toast("goMain".invokeToString())
        "goHome".invokeToString("顺丰到付都是")

    }
}

val richData = """

    <p>电饭锅发的</p>
    
<img src="https://img0.baidu.com/it/u=1275095085,1961143463&fm=253&fmt=auto&app=120&f=JPEG?w=1280&h=800" />

    <p>东风股份电饭锅电饭锅电饭锅电饭锅电饭锅电饭锅电饭锅的给大家看的房间开始的道路上防溺水大龙方女生的防溺水的看法你的看法建设大街防溺水答复你死定了妇女水电费双氯芬酸钠浪费</p>
"""